import 'dart:async';
import 'dart:convert';
import 'package:logger/logger.dart';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/video.dart';
import 'package:qgchat/http/models/invitationInfoModel.dart';
import 'package:qgchat/http/models/taglistModel.dart';

class LiveEndBloc implements BlocBase {
  // ##########  STREAMS  ##############

  /// 通话记录详情
  StreamController _videoData =
      StreamController<InvitationInfoModel>.broadcast();
  Sink get _videoDataSink => _videoData.sink;
  Stream get videoDataStream => _videoData.stream;

  /// 评价标签列表
  StreamController _taglist = StreamController<List<TaglistModel>>.broadcast();
  Sink get _taglistSink => _taglist.sink;
  Stream get taglistStream => _taglist.stream;

  /// 选择评价标签
  StreamController _choiceTag = StreamController<TaglistModel>.broadcast();
  Sink get _choiceTagSink => _choiceTag.sink;
  Stream get choiceTagStream => _choiceTag.stream;

  // ##########  DEFAULT  ##############

  final InvitationInfoModel videoDataDefault = InvitationInfoModel(
    gold: 0,
    giftGlod: 0,
    endtime: '0',
    talktime: '0',
  );

  // ##########  HANDLING  ##############

  String _targetToken;

  LiveEndBloc(int inv, String targetToken) {
    _targetToken = targetToken;
    taglist();
    getVideoData(inv);
  }

  void dispose() {
    _videoData.close();
    _taglist.close();
    _choiceTag.close();
  }

  /// 当前选择的标签
  List<TaglistModel> choiceTagList = [];

  /// 获取通话记录详情
  void getVideoData(int inv) async {
    if (_videoData.isClosed) return;
    InvitationInfoModel result = await VideoApis.invitationInfo(inv);
    if (_videoData.isClosed) return;
    if (result.id == null || result.giftGlod == null || result.endtime == '0') {
      Logger().i(result.toJson());
      Timer(Duration(milliseconds: 500), () {
        getVideoData(inv);
      });
    } else {
      _videoDataSink.add(result);
    }
  }

  /// 评价标签列表
  void taglist() async {
    List<TaglistModel> result = await VideoApis.taglist();
    if (!_taglist.isClosed) {
      _taglistSink.add(result);
    }
  }

  /// 选择评价标签
  void choiceTag(TaglistModel item) {
    if (choiceTagList.length > 2) {
      showToast("只能选择最多三个标签");
    } else if (!choiceTagList.contains(item)) {
      _choiceTagSink.add(item);
      choiceTagList.add(item);
    }
  }

  /// 删除已选择标签
  void delTag(int id) {
    int i = choiceTagList.indexWhere((item) => item.id == id);
    if (i == -1) {
      return;
    }
    _choiceTagSink.add(choiceTagList[i]);
    choiceTagList.removeAt(i);
  }

  /// 确定评价
  void commentTag(int zhuboId) async {
    List<int> xingxiangId = choiceTagList.map((item) => item.id).toList();
    Map result = await VideoApis.commentZhubo(zhuboId, xingxiangId);
    if (result['status'] == 1) {
      showToast('评价成功');
    }
  }

  // 举报主播
  reportAnthor(content) async {
    Map result = await VideoApis.reportAnthor(_targetToken, content);
    if (result['status'] == 1) {
      showToast("举报成功，官方会马上核实");
    } else {
      showToast(result['msg']);
    }
  }
}
